package cms.simmytech.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

	public static final String SYMBOL_SEP = " ";

	private static final String MONTH_STR = "yyyy-MM";

	private static final String DATE_STR = "yyyy-MM-dd";

	private static final String TIME_STR = "HH:mm:ss";

	private static final String DATETIME_STR = "yyyy-MM-dd HH:mm:ss";

	private static final String DATEHOUR_STR = "yyyy-MM-dd HH";

	private static final String HOUR_MINUTE_STR = "HH:mm";

	private static final String DATETIME_SECOND_STR = "yyyyMMddHHmmssSSS";

	private static final String DATETIME_SECOND_STR1 = "yyyyMMddHHmmss";

	private static final String DATE_SINGLE_STR = "d";

	private static final String YEAR_DATE_STR = "yyyy-MM-dd";

	public static final String getMonthDateString(int dateInt) {
		long time = (long) dateInt * 1000;
		String dayString = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(YEAR_DATE_STR);
			Date date = new Date(time);
			dayString = sdf.format(date);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return dayString;

	}

	/** 获取当前时间0点时间 戳 */
	public static final String timeStringAfter(int baseTimeSecond, int secondAfter) {
		int intTimeAfter = baseTimeSecond + secondAfter;
		long currentTime = (long) intTimeAfter * 1000;
		Date date = new Date(currentTime);
		DateFormat df = new SimpleDateFormat(DATETIME_SECOND_STR1);
		String nowTime = "";
		nowTime = df.format(date);
		System.out.println(nowTime);
		return nowTime;
	}

	public static String getDateTime(String format, long timeMillis) {
		SimpleDateFormat sd = new SimpleDateFormat(format);
		String date = sd.format(new Date(timeMillis));
		return date;
	}

	public static String getNowDate(String format) {
		SimpleDateFormat sd = new SimpleDateFormat(format);
		String date = sd.format(new Date(System.currentTimeMillis()));
		return date;
	}



	/**
	 * 计算两个日期差多少天
	 * 
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@SuppressWarnings("unused")
	public static long dateDiff(String startTime, String endTime) {
		// 按照传入的格式生成一个simpledateformate对象
		SimpleDateFormat sd = new SimpleDateFormat(DATE_YYYYMMDD);
		long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
		long nh = 1000 * 60 * 60;// 一小时的毫秒数
		long nm = 1000 * 60;// 一分钟的毫秒数
		long ns = 1000;// 一秒钟的毫秒数
		long diff;
		try {
			// 获得两个时间的毫秒时间差异
			diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
			long day = diff / nd;// 计算差多少天
			return day;
		}
		catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 计算两个日期差多少小时
	 *
	 * @param endTime
	 * @return
	 * @throws ParseException
	 */
	@SuppressWarnings("unused")
	public static long hourDiff(Date nowTime, Date endTime) throws ParseException {
		// 按照传入的格式生成一个simpledateformate对象
		long l = endTime.getTime() - nowTime.getTime();
		long hour = l / (60 * 60 * 1000);
		return hour;
	}

	/**
	 * 计算两个日期差多少小时
	 *
	 * @param endTime
	 * @return
	 * @throws ParseException
	 */
	@SuppressWarnings("unused")
	public static long hourDiff(String nowTime, String endTime) throws ParseException {
		// 按照传入的格式生成一个simpledateformate对象
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		java.util.Date now = df.parse(nowTime);

		java.util.Date date = df.parse(endTime);
		long l = date.getTime() - now.getTime();
		long hour = l / (60 * 60 * 1000);
		return hour;
	}

	/**
	 * @DESC
	 * @param date
	 * @param mill
	 * @return
	 * @throws ParseException
	 */
	public static Date getDateAddMill(Date date, long mill) throws ParseException {
		long l = date.getTime() + mill;
		return new Date(l);
	}

	/**
	 * @DESC 相差的毫秒数
	 * @param endTime
	 * @return
	 * @throws ParseException
	 */
	public static long millDiff(Date startTime, Date endTime) {
		long l = startTime.getTime() - endTime.getTime();
		return l;
	}

	/**
	 * 获取日期数组 如果两个日期之间的日子差平均分为 iNum个日期
	 * 
	 * @param startTime
	 * @param endTime
	 * @param iNum
	 * @return
	 */
	public static String[] dateDiff(String startTime, String endTime, int iNum) {
		// Rtn
		String sArray = "";
		SimpleDateFormat sd = new SimpleDateFormat(DATE_YYYYMMDD);
		try {
			// 相差天数
			int day = Integer.parseInt(String.valueOf(dateDiff(startTime, endTime)));
			// 间隔
			int isep = 0;
			if (day <= 0) {
				sArray = startTime + ";";
			} else if (day < iNum) {
				for (int i = 0; i <= day; i++) {
					sArray = sArray + endTime + ";";
					endTime = getSpecifiedDayBefore(endTime);
				}
			} else {
				if (day % iNum == 0) {
					isep = day / iNum;
				} else {
					isep = (day / iNum + 1);
				}
				while (true) {
					sArray = sArray + endTime + ";";
					endTime = getSpecifiedDayBefore(endTime, isep);
					if (sd.parse(endTime).getTime() <= sd.parse(startTime).getTime()) {
						break;
					}
				}
			}
			String[] array = sArray.split(";");
			return array;

		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@SuppressWarnings("unused")
	public static long dateDiff(String startTime, String endTime, String format) {
		// 按照传入的格式生成一个simpledateformate对象
		SimpleDateFormat sd = new SimpleDateFormat(DATE_YYYYMMDD);
		long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
		long nh = 1000 * 60 * 60;// 一小时的毫秒数
		long nm = 1000 * 60;// 一分钟的毫秒数
		long ns = 1000;// 一秒钟的毫秒数
		long diff;
		try {
			// 获得两个时间的毫秒时间差异
			diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
			long day = diff / nd;// 计算差多少天
			return day;
		}
		catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}

	@SuppressWarnings("unused")
	public static boolean compareTime(String startTime, String endTime, String format) {
		// 按照传入的格式生成一个simpledateformate对象
		SimpleDateFormat sd = new SimpleDateFormat(format);
		long diff;
		try {
			// 获得两个时间的毫秒时间差异
			diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
			return diff < 0 ? true : false;
		}
		catch (ParseException e) {
			e.printStackTrace();
			return false;
		}
	}

	private static final String DATE_YYYYMMDD = "yyyy-MM-dd";

	public static Date str2Date(String sText) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_YYYYMMDD);
		try {
			return sdf.parse(sText);
		}
		catch (ParseException e) {
			//
		}
		return null;
	}

	/**
	 * 获得指定日期的前一天
	 * 
	 * @param specifiedDay
	 * @return
	 * @throws Exception
	 */
	public static String getSpecifiedDayBefore(String specifiedDay) {
		// SimpleDateFormat simpleDateFormat = new
		// SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		Date date = null;
		try {
			date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
		}
		catch (ParseException e) {
			e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day - 1);

		String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
		return dayBefore;
	}

	/**
	 * 获取指定日期的前i天
	 * 
	 * @param specifiedDay
	 * @param i
	 * @return
	 */
	public static String getSpecifiedDayBefore(String specifiedDay, int i) {
		// SimpleDateFormat simpleDateFormat = new
		// SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		Date date = null;
		try {
			date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
		}
		catch (ParseException e) {
			e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day - i);

		String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
		return dayBefore;
	}

	public static String getSpecifiedFormatDayBefore(String format, int i) {
		// SimpleDateFormat simpleDateFormat = new
		// SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		Date date = new Date();
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day + i);

		String dayBefore = new SimpleDateFormat(format).format(c.getTime());
		return dayBefore;
	}

	/**
	 * 获得指定日期的后一天
	 * 
	 * @param specifiedDay
	 * @return
	 */
	public static String getSpecifiedDayAfter(String specifiedDay) {
		Calendar c = Calendar.getInstance();
		Date date = null;
		try {
			date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
		}
		catch (ParseException e) {
			e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day + 1);

		String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
		return dayAfter;
	}

	/** 将今天日期转化成 字符串 */
	public static String getTodayToStr(String sText) {
		if (sText == null || "".equals(sText)) {
			sText = "yyyy-MM-dd";
		}
		return new SimpleDateFormat(sText).format(new Date());
	}

	public static String getNowMonthToStr(String sText) {
		if (sText == null || "".equals(sText)) {
			sText = "yyyy-MM";
		}
		return new SimpleDateFormat(sText).format(new Date());
	}

	public static String getLastMonthToStr(String sText) {
		if (sText == null || "".equals(sText)) {
			sText = "yyyy-MM";
		}
		java.util.Calendar c = java.util.Calendar.getInstance();
		c.add(c.MONTH, -1);// 得到上个月的月份
		java.util.Date date = c.getTime();
		// System.out.println(date);
		return new SimpleDateFormat(sText).format(date);
	}

	public static String getNextMonthToStr(String sText) {
		java.util.Calendar c = java.util.Calendar.getInstance();

		SimpleDateFormat sdf = new SimpleDateFormat(MONTH_STR);
		int i = 0;
		Date dt = null;
		try {
			dt = sdf.parse(sText);
			c.setTime(dt);
			c.add(c.MONTH, 1);// 得到上个月的月份
			// System.out.println(date)
			java.util.Date date = c.getTime();
			return new SimpleDateFormat(MONTH_STR).format(date);
		}
		catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}

	public static String getAnyLastMonthToStr(String sText, int lastNum) {
		if (sText == null || "".equals(sText)) {
			sText = "yyyy-MM";
		}
		java.util.Calendar c = java.util.Calendar.getInstance();
		c.add(c.MONTH, lastNum);// 得到上个月的月份
		java.util.Date date = c.getTime();
		// System.out.println(date);
		return new SimpleDateFormat(sText).format(date);
	}

	/** 将指定日期转化成字符串 */
	public static String getTodayToStr(Date sDate) {
		return new SimpleDateFormat("yyyy-MM-dd").format(sDate);
	}

	/** 将指定日期转化成字符串 */
	public static String getDateToStr(Date sDate) {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(sDate);
	}

	public static String getDateStr(String sDate) {
		return new SimpleDateFormat("yyyyMMddHHmmss").format(sDate);
	}

	/** 获取当前日期,格式:<code>yyyy-MM-dd</code> */
	public static final String getDate() {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_STR);
		String str = sdf.format(new Date());
		return str;
	}

	public static final String getMonth() {
		SimpleDateFormat sdf = new SimpleDateFormat(MONTH_STR);
		String str = sdf.format(new Date());
		return str;
	}

	public static final int getDateInt(String dateString) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_STR);
		Date dt = null;
		try {
			dt = sdf.parse(dateString);
		}
		catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (int) (dt.getTime() / 1000);
	}

	public static final int getDateInt(){
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_STR);
		Date dt=null;
		try{
			String dateString= sdf.format(new Date());
			dt=sdf.parse(dateString);
		}catch(ParseException e){
			e.printStackTrace();
		}
		return (int) (dt.getTime() / 1000);
	}

	public static final int getDateTimeInt(String dateString) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_STR);
		Date dt = null;
		try {
			dt = sdf.parse(dateString);
		}
		catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (int)(dt.getTime()/1000);
	}

	public static final int getMonthInt(String dateString) {
		SimpleDateFormat sdf = new SimpleDateFormat(MONTH_STR);
		int i = 0;
		Date dt = null;
		try {
			dt = sdf.parse(dateString);
			long time = dt.getTime();
			i = (int) (time / 1000);
		}
		catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}

	public static final int getNextMonthInt(String dateString) {
		SimpleDateFormat sdf = new SimpleDateFormat(MONTH_STR);
		int i = 0;
		Date dt = null;
		try {
			String nextMonthString = getNextMonthToStr(dateString);
			dt = sdf.parse(nextMonthString);
			long time = dt.getTime();
			i = (int) (time / 1000);
		}
		catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}

	/** 获取当前时间,格式:<code>HH:mm:ss</code> */
	public static final String getTime() {
		SimpleDateFormat sdf = new SimpleDateFormat(TIME_STR);
		String str = sdf.format(new Date());
		return str;
	}

	/** 获取当前日期时间,格式:<code>yyyy-MM-dd HH:mm:ss</code> */
	public static final String getDateTime() {
		SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_STR);
		String str = sdf.format(new Date());
		return str;
	}

	public static final String getNowStr() {
		SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_STR);
		String sdate = sdf.format(Calendar.getInstance().getTime());
		sdate = sdate.replace("-", "");
		sdate = sdate.replace(":", "");
		sdate = sdate.replace(" ", "");
		return sdate;
	}

	/** 获取日期,说明: n=0为当日, 1为明天, -1为昨天 */
	public static final String getDate(int n) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_YEAR, n);
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_STR);
		String str = sdf.format(calendar.getTime());
		return str;
	}

	public static final String getDate(Calendar calendar) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_STR);
		String str = sdf.format(calendar.getTime());
		return str;
	}

	/** 获取时间,说明: n=0为当前小时, 1为后一小时, -1为前一小时 */
	public static final Calendar getHour(int n) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.HOUR_OF_DAY, n);
		return calendar;
	}

	/** 获取小时 */
	public static final int getHour(Calendar calendar) {
		return calendar.get(Calendar.HOUR_OF_DAY);
	}

	/**
	 * 获取当前日期+小时,格式:<code>yyyy-MM-dd HH</code>
	 * 
	 * @param n
	 *            小时, n=0为当前小时, 1为后一小时, -1为前一小时
	 * @return
	 */
	public static final String getDateAndHour(int n) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.HOUR_OF_DAY, n);

		SimpleDateFormat sdf = new SimpleDateFormat(DATEHOUR_STR);
		String str = sdf.format(calendar.getTime());
		return str;
	}

	public static Timestamp timeStringToTimeStamp(String timeString) {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		format.setLenient(false);
		Timestamp ts = null;
		;
		try {
			ts = new Timestamp(format.parse(timeString).getTime());
			// System.out.println(ts.toString());
			return ts;
		}
		catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ts;
	}

	public static Date timeStringToDate(String timeString) {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		format.setLenient(false);
		Date date = null;
		;
		try {
			date = new Date(format.parse(timeString).getTime());
			// System.out.println(ts.toString());
			return date;
		}
		catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}



	public static long StringToStamp(String timeString) {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		format.setLenient(false);
		long ts = 0;
		;
		try {
			ts = format.parse(timeString).getTime();
			// System.out.println(ts.toString());
			return ts;
		}
		catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ts;
	}

	/**
	 * 时间戳转字符串时间
	 * 
	 * @param s
	 * @return
	 */
	public static String stampToDate(String s) {
		// s = 1479285979 + "";
		String res;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		long lt = new Long(s);
		Date date = new Date(lt * 1000);
		res = simpleDateFormat.format(date);
		return res;
	}

	public static Integer getIntStampTime() {
		Integer intTime;
		Date date = new Date();
		long time = System.currentTimeMillis();
		String strTime = String.valueOf(time / 1000);
		intTime = Integer.valueOf(strTime);
		return intTime;
	}

	public static String intToStringStampTime(int intTime) {
		long time = (long) intTime * 1000;
		SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_STR);
		Date date = new Date(time);
		return sdf.format(date);
	}

	public static String intToStringDateTime(int intTime) {
		long time = (long) intTime * 1000;
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_STR);
		Date date = new Date(time);
		return sdf.format(date);
	}

	public static String intToStringSingleDate(int intTime) {
		long time = (long) intTime * 1000;
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_SINGLE_STR);
		Date date = new Date(time);
		return sdf.format(date);
	}

	public static long stringToStringDateTime(String intTime) {
		SimpleDateFormat sdf = new SimpleDateFormat(HOUR_MINUTE_STR);
		long time = 0;
		try {
			time = sdf.parse(intTime).getTime();
		}
		catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return time;
	}

	public static int stringDateTimeToInt(String intTime) {
		SimpleDateFormat sdf = new SimpleDateFormat(HOUR_MINUTE_STR);
		int intDateTime = -5000;
		try {
			long time = sdf.parse(intTime).getTime();
			String strTime = String.valueOf(time / 1000);
			intDateTime = Integer.valueOf(strTime);
		}
		catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return intDateTime;
	}

	public static String intToStringHourMinute(int intTime) {
		long time = (long) intTime * 1000;
		SimpleDateFormat sdf = new SimpleDateFormat(HOUR_MINUTE_STR);
		Date date = new Date(time);
		return sdf.format(date);
	}

	public static String transferLongToDate(Long millSec) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_STR);
		Date date = new Date(millSec);
		return sdf.format(date);
	}

	public static String getAfterMonth(String inputDate, int number) {
		Calendar c = Calendar.getInstance();// 获得一个日历的实例
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(inputDate);// 初始日期
		}
		catch (Exception e) {

		}
		c.setTime(date);// 设置日历时间
		c.add(Calendar.MONTH, number);// 在日历的月份上增加6个月
		String strDate = sdf.format(c.getTime());// 的到你想要得6个月后的日期
		return strDate;
	}

	/** 获取当前日期时间,格式:<code>yyyy-MM-dd HH:mm:ss</code> */
	public static final String getDateTimeSecond() {
		SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_SECOND_STR);
		String str = sdf.format(new Date());
		return str;
	}


	/**
	 * 字符串时间转时间戳
	 *
	 * @param timeString
	 *            yyyy-MM-dd HH:mm:ss
	 * @return 1502250326
	 */
	public static int StringToTimeStamp(String timeString) {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		format.setLenient(false);
		int time = 0;
		try {
			long ts = format.parse(timeString).getTime();
			time = (int) (ts / 1000);
		}
		catch (ParseException e) {
			e.printStackTrace();
		}

		return time;
	}

	public static int StringToTimeYMDHMStamp(String timeString){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		format.setLenient(false);
		int time = 0;
		try {
			long ts = format.parse(timeString).getTime();
			time = (int) (ts / 1000);
		}
		catch (ParseException e) {
			e.printStackTrace();
		}

		return time;
	}
	public static String intToStringYMDHMString(Integer timeStamp){
		long time = (long) timeStamp * 1000;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date date = new Date(time);
		return sdf.format(date);
	}
	public static void main(String[] args) {
		System.out.println(stringToStringDateTime("11:24"));
		System.out.println(stringDateTimeToInt("08:00"));
		System.out.println(getDateTimeInt("2021-04-09 20:16:20"));
		System.out.println(StringToTimeStamp("2021-04-09 20:16:20"));


	}
}
